package brickdestroyer.simulation;

import brickdestroyer.math.Vector2D;
import java.awt.Graphics;
import static brickdestroyer.simulation.Simulation.*;

/**
 * This class manages the slider in the game.
 *
 * @author Momchil Atanasov
 */
public final class SliderManager {
          
    private static final float SLIDER_COLLISION_SIDE_RATIO = 0.05f;

    private static final Vector2D SLIDER_COLLISION_VERTICAL_CHANGE = new Vector2D(0.0f, - 2.0f);

    private static final Vector2D DEFAULT_SLIDER_POSITION = new Vector2D(PLAYABLE_AREA_CENTER_X,
            PLAYABLE_AREA_HEIGHT - Slider.DEFAULT_SLIDER_HEIGHT / 2.0f);
   
    private final Slider slider = new Slider();
       
    public SliderManager() {
        slider.setTo(DEFAULT_SLIDER_POSITION);
    }

    /**
     * Returns a reference to the game slider.
     * @return reference to the slider object.
     */
    public Slider getSlider() {
        return slider;
    }

    /**
     * This method toggles the state of the slider.
     */
    public void toggle() {
        slider.toggle();
    }

    /**
     * This method sets a new X coordinate of the slider.
     * @param x new X slider coordinate.
     */
    public void setPosition(float x) {
        if (x < PLAYABLE_AREA_LEFT + slider.getHalfWidth()) {
            x = PLAYABLE_AREA_LEFT + slider.getHalfWidth();
        }
        if (x > PLAYABLE_AREA_RIGHT - slider.getHalfWidth()) {
            x = PLAYABLE_AREA_RIGHT - slider.getHalfWidth();
        }
        slider.x = x;
    }

    /**
     * This method returns the X coordinate of the slider.
     * @return X coordinate of the slider.
     */
    public float getPosition() {
        return slider.x;
    }

    /**
     * This method determines what happens when a ball
     * hit's the slider.
     * @param ball ball hitting the slider.
     */
    public void onHit(Ball ball) {
        if (slider.isActive()) {
            ball.getSpeed().inc(SLIDER_COLLISION_VERTICAL_CHANGE);
        }
        ball.getSpeed().x += (ball.x - slider.x) * SLIDER_COLLISION_SIDE_RATIO;
    }

    /**
     * This method displays the slider on the screen.
     * @param g interface to the graphics handle.
     */
    public void display(Graphics g) {
        slider.display(g);
    }
}
